﻿using Ozone.Help;
using Ozone.Views.Widget;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace HighThroughput.Common
{
    public class Tools
    {
        public static Stream getResourceStream(string resourceName)
        {
            Uri Source = new Uri(resourceName, UriKind.RelativeOrAbsolute);
            Stream stream = System.Windows.Application.GetResourceStream(Source).Stream;

            return stream;
        }

        public static SolidColorBrush getColor(string resName)
        {
            //找到资源 背景色
            var myResourceDictionaryShared = new ResourceDictionary
            {
                Source = new Uri("Resources/Styles/Shared.xaml", UriKind.RelativeOrAbsolute) // 指定样式文件的路径
                                                                                             //    Source = new Uri("Resources/Styles/Expander.xaml", UriKind.RelativeOrAbsolute) // 指定样式文件的路径
            };
            SolidColorBrush Brush_Color = myResourceDictionaryShared[resName] as SolidColorBrush; // 通过key找到指定的样式

            return Brush_Color;
        }

        public static T GetChildObjectByTag<T>(DependencyObject obj, string tag) where T : FrameworkElement
        {
            DependencyObject child = null;
            T grandChild = null;

            for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
            {
                child = VisualTreeHelper.GetChild(obj, i);

                if (child is T)
                {
                    if (((T)child).Tag != null)
                    {
                        string ttt = (((T)child).Tag.ToString());
                        if (ttt == tag)
                            return (T)child;
                    }
                }
                else
                {
                    grandChild = GetChildObjectByTag<T>(child, tag);
                    if (grandChild != null)
                        return grandChild;
                }
            }

            return null;
        }

        public static List<T> GetChildListObjectByTag<T>(DependencyObject obj, string tag) where T : FrameworkElement
        {
            DependencyObject child = null;
            T grandChild = null;
            List<T> list = new List<T>();

            for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(obj) - 1; i++)
            {
                child = VisualTreeHelper.GetChild(obj, i);

                if (child is T)
                {
                    if (((T)child).Tag != null)
                    {
                        string ttt = (((T)child).Tag.ToString());
                        if (ttt == tag || tag == "")
                            list.Add((T)child);
                    }
                }
                else
                {
                    grandChild = GetChildObjectByTag<T>(child, tag);
                    if (grandChild != null)
                        list.Add((T)grandChild);
                }
            }

            return list;
        }

        /// <summary>
        /// 通过某种类型查找子控件列表,带递归
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parentObj">某个父控件</param>
        /// <param name="typename">需要查找子控件类型</param>
        /// <returns>查找到的子控件列表</returns>
        public static List<T> GetChildObjects<T>(DependencyObject parentObj, Type typename, string tag) where T : FrameworkElement
        {
            DependencyObject child = null;
            List<T> childList = new List<T>();

            for (int i = 0; i <= VisualTreeHelper.GetChildrenCount(parentObj) - 1; i++)
            {
                child = VisualTreeHelper.GetChild(parentObj, i);

                if (child is T && ((((T)child).GetType() == typename) && (((T)child).Tag.ToString() == tag || tag == "")))
                {
                    childList.Add((T)child);
                }
                childList.AddRange(GetChildObjects<T>(child, typename, tag));
            }
            return childList;
        }

        public static string GetTextValByTag(DependencyObject obj, string tag)
        {
            TextBox txt = Tools.GetChildObjectByTag<TextBox>(obj, tag);
            if (txt != null)
                return txt.Text;
            else
                return "";
        }

        public static void SetTextValByTag(DependencyObject obj, string tag, string val)
        {
            TextBox txt = Tools.GetChildObjectByTag<TextBox>(obj, tag);
            if (txt != null)
                txt.Text = val;
        }

        public static void SetButtonValByTag(DependencyObject obj, string tag, string val)
        {
            Button txt = Tools.GetChildObjectByTag<Button>(obj, tag);
            if (txt != null)
                txt.Content = val;
        }

        public static string GetButtonValByTag(DependencyObject obj, string tag)
        {
            Button txt = Tools.GetChildObjectByTag<Button>(obj, tag);
            if (txt != null)
                return txt.Content.ToString();
            else
                return "";
        }

        //将秒数转化为时分秒
        public static string sec_to_hms(long duration)
        {
            TimeSpan ts = new TimeSpan(0, 0, Convert.ToInt32(duration));
            string str = "";
            if (ts.Hours > 0)
            {
                str = String.Format("{0:00}", ts.Hours) + ":" + String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds);
            }
            if (ts.Hours == 0 && ts.Minutes > 0)
            {
                str = "00:" + String.Format("{0:00}", ts.Minutes) + ":" + String.Format("{0:00}", ts.Seconds);
            }
            if (ts.Hours == 0 && ts.Minutes == 0)
            {
                str = "00:00:" + String.Format("{0:00}", ts.Seconds);
            }
            return str;
        }

        //是否再范围内
        public static bool IsInRang(double v, double vMin, double vMax)
        {
            if (v < vMin)
                return false;

            if (v > vMax)
                return false;

            return true;
        }

        //保留小数位
        public static double RoundPro(string v, int p = 1)
        {
            try
            {
                if (v == null)
                    return 0;

                return (double)Decimal.Round((Decimal)(OSSUtility.Utility.str2double(v)), p);
            }
            catch (Exception e)
            {
                return 0;
            }
        }

        public static double RoundPro(double v, int p = 1)
        {
            try
            {
                return (double)Decimal.Round((Decimal)(v), p);
            }
            catch (Exception e)
            {
                return (double)0;
            }
        }

        /// <summary>
        /// 显示提示框
        /// </summary>
        /// <param name="dialogType"></param>
        /// <param name="messageInfo"></param>
        public static void ShowPromptDialog(string dialogType, string messageInfo)
        {
            WindowPromptDialog windowPromptDialog = new WindowPromptDialog(dialogType, messageInfo);
            windowPromptDialog.ShowDialog();
        }

        /// <summary>
        /// 泛型枚举转成list
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<T> GetEnumList<T>() where T : Enum
        {
            List<T> list = Enum.GetValues(typeof(T)).OfType<T>().ToList();
            return list;
        }

        /// <summary>
        /// 清除保存的用户凭据 用户未点击记住密码单选框，进行清除凭据，还有重新登录系统清除凭据
        /// </summary>
        public static void ClearCredentials()
        {
            try
            {
                using (var store = IsolatedStorageFile.GetUserStoreForAssembly())
                {
                    if (store.FileExists("credentials.txt"))
                        store.DeleteFile("credentials.txt");
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("清除凭据失败", ex);
            }
        }
    }
}